<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class RechargeRebateController extends AdminbaseController {

        protected $m_recharge_rebate;

        function _initialize() {
            parent::_initialize();
            $this->m_recharge_rebate= D("Admin/RechargeRebate");
        }

        /**
         * 管理列表
         */
        public function index(){
            $count = $this->m_recharge_rebate->count();
            $page = $this->page($count, 15);
            $list = $this->m_recharge_rebate->get_list([],$page->firstRow, $page->listRows);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }


        // 添加
        public function add(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_recharge_rebate->get_one(['id'=>$id]);
                $this->assign("item",$item);
            }
            $this->display();
        }

        // 提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id",'');
                $insert = I("post.");
                $start_price = I('post.start_price');
                $end_price = I('post.end_price');
                $rebate_percent = I('post.rebate_percent');
                $start_price = round($start_price,2);
                $end_price = round($end_price,2);
                $rebate_percent = round($rebate_percent,2);
                if($end_price<$start_price){
                    $this->error("金额输入有误！");exit;
                }
                if($rebate_percent<0 || $rebate_percent>100){
                    $this->error("返利比例输入有误！");exit;
                }
                $result = $this->m_recharge_rebate->check_price($start_price,$end_price,$id);
                if($result){
                    $this->error("您输入的金额已存在！");exit;
                }

                $insert['start_price'] = $start_price;
                $insert['end_price'] = $end_price;
                $insert['rebate_percent'] = $rebate_percent;
                if(empty($id)){
                    $result =$this->m_recharge_rebate->add_data($insert);
                }else{

                    $result = $this->m_recharge_rebate->update_data($insert,$id);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        // 删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_recharge_rebate->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

    }